<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Repository;

use App\Model\GroupUser;

/**
 * User 数据层
 */
class GroupUserRepository extends GroupUser
{
    /**
     * 是否群成员.
     * @param mixed $userId
     * @param mixed $groupId
     */
    public static function checkIsGroupMember($userId, $groupId, bool $returnGroup = false): array|bool
    {
        $group = self::query()
            ->where('group_id', $groupId)
            ->where('user_id', $userId)
            ->first(['group_id', 'user_id', 'nickname'])->toArray();
        return $returnGroup ? [(bool) $group, $group] : (bool) $group;
    }

    /**
     * 获取用户的所有群ID.
     */
    public static function getUserGroupIds(int $user_id): array
    {
        return GroupUser::query()->where('user_id', $user_id)->pluck('group_id')->toArray();
    }

    /**
     * 获取所有群成员ID.
     */
    public function getGroupUserIds(int $group_id): array
    {
        return GroupUser::query()->where('group_id', $group_id)->pluck('user_id')->toArray();
    }
}
